# [stable|testing|upstream]/Containerfile
#
# Build a Buildah container image from the latest version
# of Fedora.
#
# FLAVOR defaults to stable if unset
#
# FLAVOR=stable    acquires a stable version of Buildah
#                   from the Fedoras Updates System.
# FLAVOR=testing   acquires a testing version of Buildah
#                   from the Fedoras Updates System.
# FLAVOR=upstream  acquires a testing version of Buildah
#                   from the Fedora Copr Buildsystem.
#                   https://copr.fedorainfracloud.org/coprs/rhcontainerbot/podman-next/
#
# https://bodhi.fedoraproject.org/updates/?search=buildah
#
# This image can be used to create a secured container
# that runs safely with privileges within the container.
#

FROM registry.fedoraproject.org/fedora:latest
ARG FLAVOR=stable

label "io.containers.capabilities"="CHOWN,DAC_OVERRIDE,FOWNER,FSETID,KILL,NET_BIND_SERVICE,SETFCAP,SETGID,SETPCAP,SETUID,CHOWN,DAC_OVERRIDE,FOWNER,FSETID,KILL,NET_BIND_SERVICE,SETFCAP,SETGID,SETPCAP,SETUID,SYS_CHROOT"

# When building for multiple-architectures in parallel using emulation
# it's really easy for one/more dnf processes to timeout or mis-count
# the minimum download rates.  Bump both to be extremely forgiving of
# an overworked host.
RUN echo -e "\n\n# Added during image build" >> /etc/dnf/dnf.conf && \
    echo -e "minrate=100\ntimeout=60\n" >> /etc/dnf/dnf.conf

# Don't include container-selinux and remove
# directories used by dnf that are just taking
# up space.
# TODO: rpm --setcaps... needed due to Fedora (base) image builds
#       being (maybe still?) affected by
#       https://bugzilla.redhat.com/show_bug.cgi?id=1995337#c3
RUN dnf -y makecache && \
    dnf -y update && \
    rpm --setcaps shadow-utils 2>/dev/null && \
    case "${FLAVOR}" in \
      stable) \
        dnf -y install buildah fuse-overlayfs cpp --exclude container-selinux \
      ;; \
      testing) \
        dnf -y install --enablerepo=updates-testing buildah fuse-overlayfs cpp \
            --exclude container-selinux \
      ;; \
      upstream) \
        dnf -y install 'dnf-command(copr)' --enablerepo=updates-testing && \
        dnf -y copr enable rhcontainerbot/podman-next && \
        dnf -y install buildah fuse-overlayfs \
            --exclude container-selinux \
            --enablerepo=updates-testing  \
      ;; \
      *) \
        printf "\\nFLAVOR argument must be set and valid, currently: '${FLAVOR}'\\n\\n" 1>&2 && \
        exit 1 \
      ;; \
    esac && \
    dnf -y clean all && \
    rm -rf /var/cache /var/log/dnf* /var/log/yum.*

ADD ./containers.conf /etc/containers/

# Setup internal Buildah to pass secrets/subscriptions down from host to internal container
RUN printf '/run/secrets/etc-pki-entitlement:/run/secrets/etc-pki-entitlement\n/run/secrets/rhsm:/run/secrets/rhsm\n' > /etc/containers/mounts.conf

# Copy & modify the defaults to provide reference if runtime changes needed.
# Changes here are required for running with fuse-overlay storage inside container.
RUN sed -e 's|^#mount_program|mount_program|g' \
        -e '/additionalimage.*/a "/var/lib/shared",' \
        -e 's|^mountopt[[:space:]]*=.*$|mountopt = "nodev,fsync=0"|g' \
        /usr/share/containers/storage.conf \
        > /etc/containers/storage.conf && \
    chmod 644 /etc/containers/storage.conf && \
    chmod 644 /etc/containers/containers.conf

RUN mkdir -p /var/lib/shared/overlay-images \
             /var/lib/shared/overlay-layers \
             /var/lib/shared/vfs-images \
             /var/lib/shared/vfs-layers && \
    touch /var/lib/shared/overlay-images/images.lock && \
    touch /var/lib/shared/overlay-layers/layers.lock && \
    touch /var/lib/shared/vfs-images/images.lock && \
    touch /var/lib/shared/vfs-layers/layers.lock

# Define uid/gid ranges for our user https://github.com/containers/buildah/issues/3053
RUN useradd build && \
    echo -e "build:1:999\nbuild:1001:64535" > /etc/subuid && \
    echo -e "build:1:999\nbuild:1001:64535" > /etc/subgid && \
    mkdir -p /home/build/.local/share/containers && \
    mkdir -p /home/build/.config/containers && \
    chown -R build:build /home/build
# See:  https://github.com/containers/buildah/issues/4669
# Copy & modify the config for the `build` user and remove the global
# `runroot` and `graphroot` which current `build` user cannot access,
# in such case storage will choose a runroot in `/var/tmp`.
RUN sed -e 's|^#mount_program|mount_program|g' \
        -e 's|^graphroot|#graphroot|g' \
        -e 's|^runroot|#runroot|g' \
        /etc/containers/storage.conf \
        > /home/build/.config/containers/storage.conf && \
        chown build:build /home/build/.config/containers/storage.conf

VOLUME /var/lib/containers
VOLUME /home/build/.local/share/containers

# Set an environment variable to default to chroot isolation for RUN
# instructions and "buildah run".
ENV BUILDAH_ISOLATION=chroot
